<template>
  <el-container class='home_container'>
    <!--头部区域-->
    <el-header>
      <div class='el-header-demo'>
        <img src='../assets/xiaoCoder.png' alt='' width='60px' height='60px'>
        <span>faster-framework快速开发框架</span>
      </div>
      <el-button type='info' @click='logout'>退出</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width='isCollapse ? "64px" : "200px"'>
        <div class='toggle-button' @click='toggleCollapse'>|||</div>
        <!--侧边栏菜单区-->
        <el-col :span='12'>
          <!-- :unique-opened='true' 只能展开唯一一项 -->
          <el-menu
              background-color='#333744'
              text-color='#fff'
              active-text-color='#409EFF'
              :unique-opened='false'
              :collapse=isCollapse
              :collapse-transition='true'
              :router='true'
              :default-active='activePath'
          >
            <!--一级菜单的模板区-->
            <el-submenu v-for='item in menulist' :key='item.orderNum' :index='item.orderNum+""'>
              <template slot='title'>
                <!--图标-->
                <i :class='item.icon'></i>
                <!--文本-->
                <span>{{ item.name }}</span>
              </template>
              <!--二级菜单-->
              <el-menu-item :index='"/"+subItem.url' @click='saveNameState("/"+subItem.url)'
                            v-for='subItem in item.list' :key='subItem.orderNum' >
                <template slot='title'>
                  <!--图标-->
                  <i :class='subItem.icon'></i>
                  <!--文本-->
                  <span>{{ subItem.name }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-col>
      </el-aside>
      <!--右侧内容区域-->
      <el-container>
        <el-main>
          <!--路由占位符-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  created() {
    this.getMenuList()
    // 开始运行就进行激活
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 请求后端 logOut 接口
      console.log("logout token:", this.$store.getters.getToken)
      this.$http.post("/shiro/userStatus/logout", this.$store.getters.getUser, {
        headers: {
          "token": this.$store.getters.getToken
        }
      }).then(res => {
        // debugger
        console.log("logout res:", res)
        if (res.data.data.status !== "00000") return this.$message.error("退出异常，请联系管理员")
        this.$message.success("退出成功，即将跳转到login界面");
        // 跳转到登录界面
        this.$router.replace('/login')
        // 清空 Vuex 中的 token 信息
        this.$store.commit("clearToken");
        // 清空sessionStorage中的信息
        window.sessionStorage.removeItem("store");
        this.$router.push('/login')
      }).catch(error => {
        this.$message.error("接口调用超时！")
        console.error(error)
      })
    },
    //  获取所有的菜单
    async getMenuList() {
      this.$http.get('/sys/menu/getAllMenus')
          .then(res => {
            if (res.data.code !== "00000") return this.$message.error("接口调用错误！")
            this.menulist = res.data.data;
            console.log(this.menulist);
          })
          .catch(error => {
            this.$message.error("接口调用异常！")
            console.error("/sys/menu/getAllMenus", error);
          })
    },
    // 点击按钮切换菜单的折叠和展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNameState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  data() {
    return {
      // 左侧菜单
      menulist: [],
      // 图标
      iconsObj: {
        '125': 'el-icon-s-check',
        '103': 'el-icon-copy-document',
        '101': 'el-icon-shopping-bag-2',
        '102': 'el-icon-notebook-2',
        '145': 'el-icon-data-analysis'
      },
      isCollapse: true,
      // 被激活的地址
      activePath: ''
    }
  }
}
</script>

<style lang='less' scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    img {
      height: 40px;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}

.el-main {
  background-color: #EAEDF1;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #ffffff;
  text-align: center;
  /*鼠标放上去可以变成小手*/
  cursor: pointer;
}

img {
  opacity: 0.3;
}

.el-menu:not(.el-menu--collapse) {
  width: 200px;
}
</style>
